﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using seo.Data;
using seo.dbutils;
using MySql.Data.MySqlClient;
using seo.DAO;

namespace seo.Buz
{
    class YiLookBuz
    {

        public List<YiLookChapter> GetChaptersByArt(int artid)
        {
            List<YiLookChapter> list = new List<YiLookChapter>();
            MySqlConnection conn = dbConnection.getLocalDbConnection();
            conn.Open();
            YiLookDao dao = new YiLookDao(conn);
            list = dao.GetChaptersByArt(artid);            
            return list;
        }

        public String GetContentByChapter(YiLookChapter chapter)
        {
            String content = "";
            MySqlConnection conn = dbConnection.getLocalDbConnection();
            conn.Open();
            YiLookDao dao = new YiLookDao(conn);
            content = dao.GetContentByChapter(chapter);
            return content;
        }

        public int getMaxChapterId(int artid) {
            int maxid=0;
            MySqlConnection conn = dbConnection.getLocalDbConnection();
             try
            {
                conn.Open();
                YiLookDao dao = new YiLookDao(conn);
                maxid=dao.getMaxChapterId(artid);
            }
             catch (Exception ex)
             {
                       throw ex;
             }
              finally
              {
             
                 conn.Close();
              }
              return maxid;
        }

        public int updateCatalogPageCount(YiLookCatalog catalog) {

            int updatecount = 0;
            MySqlConnection conn = dbConnection.getLocalDbConnection();
            conn.Open();
            MySqlTransaction trans = conn.BeginTransaction();
            try
            {
                YiLookDao dao = new YiLookDao(conn);
                updatecount = dao.updateCatalogPageCount(catalog);
                trans.Commit();
            }
            catch (Exception ex)
            {
                //trans.Rollback();   
                //throw ex;
            }
            finally
            {
                conn.Close();
            }
            return updatecount;
        }

        public List<YiLookCatalog> GetAllCatalog() {
            List<YiLookCatalog> list = new List<YiLookCatalog>();
            MySqlConnection conn = dbConnection.getLocalDbConnection();
            conn.Open();
            YiLookDao dao = new YiLookDao(conn);
            list = dao.GetAllCatalog();
            return list;
        
        }

        public List<YiLookArt> GetAllArts()
        {
            List<YiLookArt> list = new List<YiLookArt>();
            MySqlConnection conn = dbConnection.getLocalDbConnection();
            conn.Open();
            YiLookDao dao = new YiLookDao(conn);
            list = dao.GetAllArts();
            return list;

        }

        public List<YiLookChapter> GetUnUploadChaptersByArt(int artid)
        {
            List<YiLookChapter> list = new List<YiLookChapter>();
            MySqlConnection conn = dbConnection.getLocalDbConnection();
            conn.Open();
            YiLookDao dao = new YiLookDao(conn);
            list = dao.GetUnUploadChaptersByArt(artid);
            return list;
        }

        public int InsertArt(YiLookArt art)
        {
            int insertCount = 0;

            MySqlConnection conn = dbConnection.getLocalDbConnection();
            conn.Open();
            MySqlTransaction trans = conn.BeginTransaction();
            try
            {
                YiLookDao dao = new YiLookDao(conn);
                insertCount = dao.InsertArt(art);
                trans.Commit();
            }
            catch (Exception ex)
            {
                //trans.Rollback();   
                //throw ex;
            }
            finally
            {
                conn.Close();
            }

            return insertCount;
        }

        public int InsertArt(string url)
        {
            int insertCount = 0;
            
            MySqlConnection conn = dbConnection.getLocalDbConnection();
            conn.Open();
            MySqlTransaction trans = conn.BeginTransaction();
            try
            {
                YiLookDao dao = new YiLookDao(conn);
                insertCount = dao.InsertArt(url);
                trans.Commit();
            }
            catch (Exception ex)
            {
                //trans.Rollback();   
                //throw ex;
            }
            finally
            {
                conn.Close();
            }

            return insertCount;
        }

        public int InsertChapter(YiLookChapter chapter)
        {
            int insertCount = 0;

            MySqlConnection conn = dbConnection.getLocalDbConnection();
            conn.Open();
            MySqlTransaction trans = conn.BeginTransaction();
            try
            {
                YiLookDao dao = new YiLookDao(conn);
                insertCount = dao.InsertChapter(chapter);
                trans.Commit();
            }
            catch (Exception ex)
            {
                //trans.Rollback();   
                //throw ex;
            }
            finally
            {
                conn.Close();
            }

            return insertCount;
        }

        public int InsertContent(YiLookChapter chapter, String content)
        {
            int insertCount = 0;

            MySqlConnection conn = dbConnection.getLocalDbConnection();
            conn.Open();
            MySqlTransaction trans = conn.BeginTransaction();
            try
            {
                YiLookDao dao = new YiLookDao(conn);
                insertCount = dao.InsertContent(chapter, content);
                trans.Commit();
            }
            catch (Exception ex)
            {
                //trans.Rollback();   
                //throw ex;
            }
            finally
            {
                conn.Close();
            }

            return insertCount;
        }
    }
}
